package com.tencent.bk.sdk.iam.dto.callback.request;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class IamSearchCondition {
    private static final Logger log = LoggerFactory.getLogger(IamSearchCondition.class);
    private List<String> idList;
    private String parentResourceTypeId;
    private String parentResourceId;
    private Map<String, List<String>> attribute;
    private Long start;
    private Long length;

    public static IamSearchCondition fromReq(CallbackRequestDTO request) {
        IamSearchCondition condition = new IamSearchCondition();
        if (request.getFilter() != null) {
            FilterDTO filter = request.getFilter();
            if (filter.getParent() != null) {
                condition.setParentResourceTypeId(filter.getParent().getType());
                condition.setParentResourceId(filter.getParent().getId());
            }

            if (filter.getIdList() != null) {
                condition.setIdList((List)filter.getIdList().parallelStream().map(String::valueOf).collect(Collectors.toList()));
            }
        }

        if (request.getPage() != null) {
            condition.setStart(request.getPage().getOffset());
            condition.setLength(request.getPage().getLimit());
        } else {
            condition.setStart(0L);
            condition.setLength(10L);
        }

        return condition;
    }
}
